home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / flex / flexs237.zoo / dfa.c < prev    next >
C/C++ Source or Header  |  1990-06-28  |  27KB  |  1,076 lines

  1. /* dfa - DFA construction routines */
  2.  
  3. /*-
  4.  * Copyright (c) 1990 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant
  11.  * to contract no. DE-AC03-76SF00098 between the United States
  12.  * Department of Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. #ifndef lint
  30. static char rcsid[] =
  31.     "@(#) $Header: /usr/fsys/odin/a/vern/flex/RCS/dfa.c,v 2.7 90/06/27 23:48:15 vern Exp $ (LBL)";
  32. #endif
  33.  
  34. #include "flexdef.h"
  35.  
  36.  
  37. /* declare functions that have forward references */
  38.  
  39. void dump_associated_rules PROTO((FILE*, int));
  40. void dump_transitions PROTO((FILE*, int[]));
  41. void sympartition PROTO((int[], int, int[], int[]));
  42. int symfollowset PROTO((int[], int, int, int[]));
  43.  
  44.  
  45. /* check_for_backtracking - check a DFA state for backtracking
  46.  *
  47.  * synopsis
  48.  *     int ds, state[numecs];
  49.  *     check_for_backtracking( ds, state );
  50.  *
  51.  * ds is the number of the state to check and state[] is its out-transitions,
  52.  * indexed by equivalence class, and state_rules[] is the set of rules
  53.  * associated with this state
  54.  */
  55.  
  56. void check_for_backtracking( ds, state )
  57. int ds;
  58. int state[];
  59.  
  60.     {
  61.     if ( (reject && ! dfaacc[ds].dfaacc_set) || ! dfaacc[ds].dfaacc_state )
  62.     { /* state is non-accepting */
  63.     ++num_backtracking;
  64.  
  65.     if ( backtrack_report )
  66.         {
  67.         fprintf( backtrack_file, "State #%d is non-accepting -\n", ds );
  68.  
  69.         /* identify the state */
  70.         dump_associated_rules( backtrack_file, ds );
  71.  
  72.         /* now identify it further using the out- and jam-transitions */
  73.         dump_transitions( backtrack_file, state );
  74.  
  75.         putc( '\n', backtrack_file );
  76.         }
  77.     }
  78.     }
  79.  
  80.  
  81. /* check_trailing_context - check to see if NFA state set constitutes
  82.  *                          "dangerous" trailing context
  83.  *
  84.  * synopsis
  85.  *    int nfa_states[num_states+1], num_states;
  86.  *    int accset[nacc+1], nacc;
  87.  *    check_trailing_context( nfa_states, num_states, accset, nacc );
  88.  *
  89.  * NOTES
  90.  *    Trailing context is "dangerous" if both the head and the trailing
  91.  *  part are of variable size \and/ there's a DFA state which contains
  92.  *  both an accepting state for the head part of the rule and NFA states
  93.  *  which occur after the beginning of the trailing context.
  94.  *  When such a rule is matched, it's impossible to tell if having been
  95.  *  in the DFA state indicates the beginning of the trailing context
  96.  *  or further-along scanning of the pattern.  In these cases, a warning
  97.  *  message is issued.
  98.  *
  99.  *    nfa_states[1 .. num_states] is the list of NFA states in the DFA.
  100.  *    accset[1 .. nacc] is the list of accepting numbers for the DFA state.
  101.  */
  102.  
  103. void check_trailing_context( nfa_states, num_states, accset, nacc )
  104. int *nfa_states, num_states;
  105. int *accset;
  106. register int nacc;
  107.  
  108.     {
  109.     register int i, j;
  110.  
  111.     for ( i = 1; i <= num_states; ++i )
  112.     {
  113.     int ns = nfa_states[i];
  114.     register int type = state_type[ns];
  115.     register int ar = assoc_rule[ns];
  116.  
  117.     if ( type == STATE_NORMAL || rule_type[ar] != RULE_VARIABLE )
  118.         { /* do nothing */
  119.         }
  120.  
  121.     else if ( type == STATE_TRAILING_CONTEXT )
  122.         {
  123.         /* potential trouble.  Scan set of accepting numbers for
  124.          * the one marking the end of the "head".  We assume that
  125.          * this looping will be fairly cheap since it's rare that
  126.          * an accepting number set is large.
  127.          */
  128.         for ( j = 1; j <= nacc; ++j )
  129.         if ( accset[j] & YY_TRAILING_HEAD_MASK )
  130.             {
  131.             fprintf( stderr,
  132.              "%s: Dangerous trailing context in rule at line %d\n",
  133.                  program_name, rule_linenum[ar] );
  134.             return;
  135.             }
  136.         }
  137.     }
  138.     }
  139.  
  140.  
  141. /* dump_associated_rules - list the rules associated with a DFA state
  142.  *
  143.  * synopisis
  144.  *     int ds;
  145.  *     FILE *file;
  146.  *     dump_associated_rules( file, ds );
  147.  *
  148.  * goes through the set of NFA states associated with the DFA and
  149.  * extracts the first MAX_ASSOC_RULES unique rules, sorts them,
  150.  * and writes a report to the given file
  151.  */
  152.  
  153. void dump_associated_rules( file, ds )
  154. FILE *file;
  155. int ds;
  156.  
  157.     {
  158.     register int i, j;
  159.     register int num_associated_rules = 0;
  160.     int rule_set[MAX_ASSOC_RULES + 1];
  161.     int *dset = dss[ds];
  162.     int size = dfasiz[ds];
  163.     
  164.     for ( i = 1; i <= size; ++i )
  165.     {
  166.     register rule_num = rule_linenum[assoc_rule[dset[i]]];
  167.  
  168.     for ( j = 1; j <= num_associated_rules; ++j )
  169.         if ( rule_num == rule_set[j] )
  170.         break;
  171.  
  172.     if ( j > num_associated_rules )
  173.         { /* new rule */
  174.         if ( num_associated_rules < MAX_ASSOC_RULES )
  175.         rule_set[++num_associated_rules] = rule_num;
  176.         }
  177.     }
  178.  
  179.     bubble( rule_set, num_associated_rules );
  180.  
  181.     fprintf( file, " associated rule line numbers:" );
  182.  
  183.     for ( i = 1; i <= num_associated_rules; ++i )
  184.     {
  185.     if ( i % 8 == 1 )
  186.         putc( '\n', file );
  187.     
  188.     fprintf( file, "\t%d", rule_set[i] );
  189.     }
  190.     
  191.     putc( '\n', file );
  192.     }
  193.  
  194.  
  195. /* dump_transitions - list the transitions associated with a DFA state
  196.  *
  197.  * synopisis
  198.  *     int state[numecs];
  199.  *     FILE *file;
  200.  *     dump_transitions( file, state );
  201.  *
  202.  * goes through the set of out-transitions and lists them in human-readable
  203.  * form (i.e., not as equivalence classes); also lists jam transitions
  204.  * (i.e., all those which are not out-transitions, plus EOF).  The dump
  205.  * is done to the given file.
  206.  */
  207.  
  208. void dump_transitions( file, state )
  209. FILE *file;
  210. int state[];
  211.  
  212.     {
  213.     register int i, ec;
  214.     int out_char_set[CSIZE];
  215.  
  216.     for ( i = 0; i < csize; ++i )
  217.     {
  218.     ec = abs( ecgroup[i] );
  219.     out_char_set[i] = state[ec];
  220.     }
  221.     
  222.     fprintf( file, " out-transitions: " );
  223.  
  224.     list_character_set( file, out_char_set );
  225.  
  226.     /* now invert the members of the set to get the jam transitions */
  227.     for ( i = 0; i < csize; ++i )
  228.     out_char_set[i] = ! out_char_set[i];
  229.  
  230.     fprintf( file, "\n jam-transitions: EOF " );
  231.  
  232.     list_character_set( file, out_char_set );
  233.  
  234.     putc( '\n', file );
  235.     }
  236.  
  237.  
  238. /* epsclosure - construct the epsilon closure of a set of ndfa states
  239.  *
  240.  * synopsis
  241.  *    int t[current_max_dfa_size], numstates, accset[num_rules + 1], nacc;
  242.  *    int hashval;
  243.  *    int *epsclosure();
  244.  *    t = epsclosure( t, &numstates, accset, &nacc, &hashval );
  245.  *
  246.  * NOTES
  247.  *    the epsilon closure is the set of all states reachable by an arbitrary
  248.  *  number of epsilon transitions which themselves do not have epsilon
  249.  *  transitions going out, unioned with the set of states which have non-null
  250.  *  accepting numbers.  t is an array of size numstates of nfa state numbers.
  251.  *  Upon return, t holds the epsilon closure and numstates is updated.  accset
  252.  *  holds a list of the accepting numbers, and the size of accset is given
  253.  *  by nacc.  t may be subjected to reallocation if it is not large enough
  254.  *  to hold the epsilon closure.
  255.  *
  256.  *    hashval is the hash value for the dfa corresponding to the state set
  257.  */
  258.  
  259. int *epsclosure( t, ns_addr, accset, nacc_addr, hv_addr )
  260. int *t, *ns_addr, accset[], *nacc_addr, *hv_addr;
  261.  
  262.     {
  263.     register int stkpos, ns, tsp;
  264.     int numstates = *ns_addr, nacc, hashval, transsym, nfaccnum;
  265.     int stkend, nstate;
  266.     static int did_stk_init = false, *stk; 
  267.  
  268. #define MARK_STATE(state) \
  269.     trans1[state] = trans1[state] - MARKER_DIFFERENCE;
  270.  
  271. #define IS_MARKED(state) (trans1[state] < 0)
  272.  
  273. #define UNMARK_STATE(state) \
  274.     trans1[state] = trans1[state] + MARKER_DIFFERENCE;
  275.  
  276. #define